﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using PMisuseAn.io;

namespace PMisuseAn
{
    /// <summary>
    /// Interaction logic for AddActorWindow.xaml
    /// </summary>
    public partial class AddActorWindow : Window
    {
        ContextInterface ci_;
        Actor actor_;
        //int no_att = 1;
        public AddActorWindow(ContextInterface ci)
        {
            InitializeComponent();
            ci_ = ci;
        }

        public AddActorWindow(ContextInterface ci, Actor actor)
        {
            InitializeComponent();
            ci_ = ci;
            actor_ = actor;
            reloadActorNameTextComponents();
            reloadActorAttributesTable();
            reloadActorRolesTable();
        }


        private void actorAttribute_b_Click(object sender, RoutedEventArgs e)
        {
            if (actor_name_tb.Text == null | actor_name_tb.Text.Trim().Equals(""))
            {
                MessageBox.Show("Missing actor name...", "Error", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            } 
            AddActorAttributeWindow acw = new AddActorAttributeWindow(this);
            acw.Owner = (Window)this.Parent;
            acw.ShowDialog();
        }

        private void actorDesc_b_Click(object sender, RoutedEventArgs e)
        {
            if (actor_name_tb.Text == null | actor_name_tb.Text.Trim().Equals(""))
            {
                MessageBox.Show("Missing actor name...", "Error", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            } 

            if (actor_ == null)
            {
                actor_ = new Actor();
                actor_.clusterName = MainWindow.clusterName;
                string actorName = actor_name_tb.Text;
                actorName = actorName.TrimEnd('\n');
                actorName = actorName.TrimEnd('\r');

                actor_.name = actorName;
            }
            
            int no_rows = actor_atts_lb.Items.Count;

            for (int x = 0; x < no_rows; x++)
            {
                ListBoxItem tr = (ListBoxItem)actor_atts_lb.Items.GetItemAt(x);
                Attribute tag_id_r = (Attribute)tr.Tag;
                if (tag_id_r != null)
                {
                    actor_.addAttribute(tag_id_r);
                }
            }
            int no_rows1 = actor_roles_lb.Items.Count;
            for (int x = 0; x < no_rows1; x++)
            {
                ListBoxItem tr = (ListBoxItem)actor_roles_lb.Items.GetItemAt(x);
                string tag_id_r = (string)tr.Tag;
                if (tag_id_r != null)
                {
                    actor_.addRole(tag_id_r);
                }
            }

            if (actor_.attributes.Count == 0)
            {
                MessageBox.Show("An actor(agent) should be associated \n" +
                "with atleast one attribute.", "Invalid Actor (agent)", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                MisUseAnObjectsDB.addActor(actor_, MainWindow.clusterName);
                ci_.loadActors();     
            }
            //close
            this.Close();
        }

        private void reloadActorNameTextComponents()
        {
            if (actor_ != null)
            {
                actor_name_tb.Text = actor_.name;
                actor_name_tb.IsEnabled = false;
            }
        }
        
        private void reloadActorAttributesTable()
        {
            if (actor_ != null)
            {
                actor_atts_lb.Items.Clear();
                
                //no_att = 1;
                foreach (Attribute attribute in actor_.attributes)
                {
                    ListBoxItem l = new ListBoxItem();
                    StackPanel s = new StackPanel();
                    s.Orientation = System.Windows.Controls.Orientation.Horizontal;

                    //ContentPresenter c1 = new ContentPresenter();
                    //c1.Content = no_att;
                    //no_att = no_att + 1;

                    ContentPresenter c2 = new ContentPresenter();

                    string att_name = attribute.name;
                    if (attribute.personallyIdentifiable)
                    {
                        att_name = att_name + "(PI)";
                    }

                    Button open_att_b = new Button();
                    open_att_b.Tag = attribute;
                    open_att_b.Style = (System.Windows.Style)FindResource("LinkButton");
                    open_att_b.Height = 15;
                    open_att_b.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                    open_att_b.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                    open_att_b.Content = att_name;
                    open_att_b.AddHandler(Button.ClickEvent, new RoutedEventHandler(open_att_b_Click));

                    c2.Content = open_att_b;

                    if (actor_atts_lb.Items.Count % 2 == 0)
                    {
                        l.Background = Brushes.AliceBlue;
                    }

                    //c1.Width = 15;
                    c2.Width = actor_atts_lb.Width - 15;
                    //s.Children.Add(c1);
                    s.Children.Add(c2);
                                        
                    ContextMenu cm = new System.Windows.Controls.ContextMenu();
                    cm.Height = 64;
                    cm.Width = 136;

                    MenuItem cm_d = new MenuItem();
                    cm_d.Header = "Delete";
                    cm_d.Tag = attribute;
                    cm_d.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(del_act_att_b_Click));
                    cm_d.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                    cm_d.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Right;
                    cm_d.FontSize = 12;
                    cm_d.Foreground = Brushes.DarkGreen;

                    BitmapImage cm_d_image = new BitmapImage();
                    cm_d_image.BeginInit();
                    Uri cm_d_Uri = new Uri("/PMisuseAn;component/images/DeleteHS.png", UriKind.RelativeOrAbsolute);
                    cm_d_image.UriSource = cm_d_Uri;
                    cm_d_image.EndInit();

                    Image cm_d_iconImage = new Image();
                    cm_d_iconImage.Source = cm_d_image;
                    cm_d.Icon = cm_d_iconImage;

                    cm.Items.Add(cm_d);
                    l.ContextMenu = cm;

                    l.Content = s;
                    l.Tag = attribute;

                    actor_atts_lb.Items.Add(l);
                }
            }
        }

        private void reloadActorRolesTable()
        {
            if (actor_ != null)
            {
                actor_roles_lb.Items.Clear();

                //no_att = 1;
                foreach (string role in actor_.roles)
                {
                    ListBoxItem l = new ListBoxItem();
                    StackPanel s = new StackPanel();
                    s.Orientation = System.Windows.Controls.Orientation.Horizontal;

                    //ContentPresenter c1 = new ContentPresenter();
                    //c1.Content = no_att;
                    //no_att = no_att + 1;

                    ContentPresenter c2 = new ContentPresenter();
                    c2.Content = role;

                    if (actor_atts_lb.Items.Count % 2 == 0)
                    {
                        l.Background = Brushes.AliceBlue;
                    }

                    //c1.Width = 15;
                    c2.Width = actor_atts_lb.Width - 15;
                    //s.Children.Add(c1);
                    s.Children.Add(c2);

                    ContextMenu cm = new System.Windows.Controls.ContextMenu();
                    cm.Height = 64;
                    cm.Width = 136;

                    MenuItem cm_d = new MenuItem();
                    cm_d.Header = "Delete";
                    cm_d.Tag = role;
                    cm_d.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(del_act_role_b_Click));
                    cm_d.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                    cm_d.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Right;
                    cm_d.FontSize = 12;
                    cm_d.Foreground = Brushes.DarkGreen;

                    BitmapImage cm_d_image = new BitmapImage();
                    cm_d_image.BeginInit();
                    Uri cm_d_Uri = new Uri("/PMisuseAn;component/images/DeleteHS.png", UriKind.RelativeOrAbsolute);
                    cm_d_image.UriSource = cm_d_Uri;
                    cm_d_image.EndInit();

                    Image cm_d_iconImage = new Image();
                    cm_d_iconImage.Source = cm_d_image;
                    cm_d.Icon = cm_d_iconImage;

                    cm.Items.Add(cm_d);
                    l.ContextMenu = cm;

                    l.Content = s;
                    l.Tag = role;

                    actor_roles_lb.Items.Add(l);
                }
            }
        }

        public void addActorAttributesTable(Attribute att)
        {
            if (att != null)
            {
                ListBoxItem l = new ListBoxItem();
                StackPanel s = new StackPanel();
                s.Orientation = System.Windows.Controls.Orientation.Horizontal;

                //ContentPresenter c1 = new ContentPresenter();
                //c1.Content = no_att;
                //no_att = no_att + 1;

                ContentPresenter c2 = new ContentPresenter();

                string att_name = att.name;
                if (att.personallyIdentifiable)
                {
                    att_name = att_name + "(PI)";
                }

                Button open_att_b = new Button();
                open_att_b.Tag = att;
                open_att_b.Style = (System.Windows.Style)FindResource("LinkButton");
                open_att_b.Height = 15;
                open_att_b.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                open_att_b.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                open_att_b.Content = att_name;
                open_att_b.AddHandler(Button.ClickEvent, new RoutedEventHandler(open_att_b_Click));
                
                c2.Content = open_att_b;

                if (actor_atts_lb.Items.Count % 2 == 0)
                {
                    l.Background = Brushes.AliceBlue;
                }

                //c1.Width = 15;
                c2.Width = actor_atts_lb.Width - 15;
                //s.Children.Add(c1);
                s.Children.Add(c2);

                ContextMenu cm = new System.Windows.Controls.ContextMenu();
                cm.Height = 64;
                cm.Width = 136;

                MenuItem cm_d = new MenuItem();
                cm_d.Header = "Delete";
                cm_d.Tag = att;
                cm_d.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(del_act_att_b_Click));
                cm_d.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                cm_d.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Right;
                cm_d.FontSize = 12;
                cm_d.Foreground = Brushes.DarkGreen;

                BitmapImage cm_d_image = new BitmapImage();
                cm_d_image.BeginInit();
                Uri cm_d_Uri = new Uri("/PMisuseAn;component/images/DeleteHS.png", UriKind.RelativeOrAbsolute);
                cm_d_image.UriSource = cm_d_Uri;
                cm_d_image.EndInit();

                Image cm_d_iconImage = new Image();
                cm_d_iconImage.Source = cm_d_image;
                cm_d.Icon = cm_d_iconImage;

                cm.Items.Add(cm_d);
                l.ContextMenu = cm;

                l.Content = s;
                l.Tag = att;

                actor_atts_lb.Items.Add(l);
            }
        }

        public void addActorRolesTable(string roleName)
        {
            if (roleName != null)
            {
                ListBoxItem l = new ListBoxItem();
                StackPanel s = new StackPanel();
                s.Orientation = System.Windows.Controls.Orientation.Horizontal;

                //ContentPresenter c1 = new ContentPresenter();
                //c1.Content = no_att;
                //no_att = no_att + 1;

                ContentPresenter c2 = new ContentPresenter();
                
                c2.Content = roleName;

                if (actor_roles_lb.Items.Count % 2 == 0)
                {
                    l.Background = Brushes.AliceBlue;
                }

                //c1.Width = 15;
                c2.Width = actor_roles_lb.Width - 15;
                //s.Children.Add(c1);
                s.Children.Add(c2);

                ContextMenu cm = new System.Windows.Controls.ContextMenu();
                cm.Height = 64;
                cm.Width = 136;

                MenuItem cm_d = new MenuItem();
                cm_d.Header = "Delete";
                cm_d.Tag = roleName;
                cm_d.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(del_act_role_b_Click));
                cm_d.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                cm_d.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Right;
                cm_d.FontSize = 12;
                cm_d.Foreground = Brushes.DarkGreen;

                BitmapImage cm_d_image = new BitmapImage();
                cm_d_image.BeginInit();
                Uri cm_d_Uri = new Uri("/PMisuseAn;component/images/DeleteHS.png", UriKind.RelativeOrAbsolute);
                cm_d_image.UriSource = cm_d_Uri;
                cm_d_image.EndInit();

                Image cm_d_iconImage = new Image();
                cm_d_iconImage.Source = cm_d_image;
                cm_d.Icon = cm_d_iconImage;

                cm.Items.Add(cm_d);
                l.ContextMenu = cm;

                l.Content = s;
                l.Tag = roleName;

                actor_roles_lb.Items.Add(l);
            }
        }

        private bool containInPara(Paragraph p4, string s_str)
        {
            bool contained = false;
            TextRange allTextRange = new TextRange(p4.ContentStart, p4.ContentEnd);
            string p_s = allTextRange.Text;
            if (p_s.Contains(s_str))
            {
                contained = true;
            }

            return contained;
        }

        void del_act_att_b_Click(object sender, RoutedEventArgs e)
        {
            MenuItem d_b = (MenuItem)sender;
            Attribute tag_id = (Attribute)d_b.Tag;

            int no_rows = actor_atts_lb.Items.Count;
            for (int x = 0; x < no_rows; x++)
            {
                ListBoxItem tr = (ListBoxItem)actor_atts_lb.Items.GetItemAt(x);
                Attribute tag_id_r = (Attribute)tr.Tag;
                if ((tag_id_r != null && tag_id != null) && tag_id_r.name.Equals(tag_id.name))
                {
                    MessageBoxResult result = MessageBox.Show("Confirm delete...", "Delete", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (result == MessageBoxResult.No)
                    {
                        return;
                    }

                    actor_atts_lb.Items.Remove(actor_atts_lb.Items.GetItemAt(x));

                    string actorName = actor_name_tb.Text.Trim();
                    actorName = actorName.TrimEnd('\n');
                    actorName = actorName.TrimEnd('\r');

                    MisUseAnObjectsDB.deleteActorAttribute(actorName, tag_id_r.name, MainWindow.clusterName);                    
                    break;
                }
            }
        }

        void del_act_role_b_Click(object sender, RoutedEventArgs e)
        {
            MenuItem d_b = (MenuItem)sender;
            string tag_id = (string)d_b.Tag;

            int no_rows = actor_roles_lb.Items.Count;
            for (int x = 0; x < no_rows; x++)
            {
                ListBoxItem tr = (ListBoxItem)actor_roles_lb.Items.GetItemAt(x);
                string tag_id_r = (string)tr.Tag;
                if ((tag_id_r != null && tag_id != null) && tag_id_r.Equals(tag_id))
                {
                    MessageBoxResult result = MessageBox.Show("Confirm role delete...", "Delete", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (result == MessageBoxResult.No)
                    {
                        return;
                    }

                    actor_roles_lb.Items.Remove(actor_roles_lb.Items.GetItemAt(x));
                    actor_.removeRole(tag_id_r);
                    string actorName = actor_name_tb.Text.Trim();
                    actorName = actorName.TrimEnd('\n');
                    actorName = actorName.TrimEnd('\r');

                    MisUseAnObjectsDB.deleteActorRole(actorName, tag_id_r, MainWindow.clusterName);
                    break;
                }
            }
        }

        public void open_att_b_Click(object sender, RoutedEventArgs e)
        {
            Button a_b = (Button)sender;
            Attribute att = (Attribute)a_b.Tag;
            Attribute full_att = MisUseAnObjectsDB.loadAttributeFull(att.name, MainWindow.clusterName);
            AddActorAttributeWindow acw = new AddActorAttributeWindow(this, full_att);
            acw.Owner = (Window)this.Parent;
            acw.ShowDialog();
        }

        private void newActorRole_b_Click(object sender, RoutedEventArgs e)
        {
            
            if (actor_name_tb.Text == null | actor_name_tb.Text.Trim().Equals(""))
            {
                MessageBox.Show("Missing actor name...", "Error", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            } 
            string roleName = actorRole_tb.Text;

            if (roleName == null || roleName.Count() == 0)
            {
                MessageBoxResult result = MessageBox.Show("Role name required", "Missing Role", MessageBoxButton.OK, MessageBoxImage.Information);
                if (result == MessageBoxResult.No)
                {
                    return;
                }
            }
            actorRole_tb.Text = "";
            addActorRolesTable(roleName);
        }
    }
}
